package com.inter.law.register;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSONObject;

import com.inter.law.dao.IlLawerDAO;
import com.inter.law.entity.IlLawer;
import com.inter.law.utils.PinyinUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class RegisterAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

     Map<String, Object> session = ActionContext.getContext().getSession();
	private JSONObject jsonObj = new JSONObject();
	private String json;
	Random rnd = new Random();
	private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	private Integer lawer_id;
	private String phonenumber;
    private String vertification;
	private String password;
	private String emial;
	private String lawyername;
	private String license;
	private String idnumber;
	private String lawfirm;//律师律所
	private String province;
	private String city;
	public String  getvertification() throws Exception {   //获取验证码action
		
//			我没记错的话 get方式提交的参数编码，只支持iso8859-1编码。
//			如果非要用这种方式的话建议：
		int vertification = rnd.nextInt(899999)+100000;
		String para = String.valueOf(vertification);
		String para2 = para+",2";
		System.out.print(para+" "+phonenumber+'\n');
		para2=RestTest.testTemplateSMS(phonenumber,para2);
		
	    session.put("ver",para);
	    jsonObj.put("respCode", para2);
	    json = jsonObj.toString();
	 //   System.out.println(json);
		return SUCCESS;
	}
	public String  jumptologin() throws Exception {	 				//返回SUCCESS 用于action跳转页面
	return SUCCESS;
}
	
public String  registerforourlawyer() throws Exception {                           //本所律师注册
	IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ac);
	ArrayList list=(ArrayList) ilLawerDAO.findByLawerIdcardnumber(license); 
	if(list!=null&&!list.isEmpty()){ 						//删除原纪录
		ilLawerDAO.delete((IlLawer) list.get(0));   
		((IlLawer) list.get(0)).setLawerPhone(phonenumber);  //修改原纪录
		((IlLawer) list.get(0)).setLawerPassword(password);
		((IlLawer) list.get(0)).setLawerUsername(phonenumber);
		ilLawerDAO.save((IlLawer) list.get(0));    //保存修改过密码和电话号码，账户名
		jsonObj.put("code", "true");
		json = jsonObj.toString();
	}
	else{
		jsonObj.put("code", "false");
		json = jsonObj.toString();
	}
	return SUCCESS;
}
public String  register() throws Exception {                    //外所律师注册
	
//	我没记错的话 get方式提交的参数编码，只支持iso8859-1编码。
//	如果非要用这种方式的话建议：
	String vertify = (String)session.get("ver");
	jsonObj.put("code", "false");
	json = jsonObj.toString();
	if(vertify!=null&&vertify.equals(vertification))
	{	
		jsonObj.put("code", "true");
		json = jsonObj.toString();
	}
	else return SUCCESS;; //验证码错误
	IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ac);
	ArrayList list=(ArrayList) ilLawerDAO.findByLawerUsername(phonenumber);  //社会律师注册时 检查 手机号作为用户名，手机号是否已经使用过
	if(list!=null&&!list.isEmpty()){
		jsonObj.put("code", "exist");
	}
	else{
		if(lawfirm.equals(0))    //获取律师事务所
		{
			lawfirm = "山东德恒律师事务所";
		}/*public IlLawer(String lawerWorknumber, String lawerUsername,
				Boolean lawerIdentify, String lawerPassword, String lawerPhone,
				String lawerName, Integer lawerAge, String lawerSex,
				String lawerEmail, String lawerIdnumber, String lawerIdcardnumber,
				String lawerFirm, String lawerProvince, String lawerCity,
				Boolean lawerManager, String lawerLocation, String lawerDeptId,
				String lawerDuty, String lawerMajor, Timestamp lawerCreateTime,
				Timestamp lawerLastLoginTime) */
		Date date = new Date();   
		System.out.println("zhuce ");
		Timestamp nousedate = new Timestamp(date.getTime());
		IlLawer lawer=new IlLawer();
		lawer.setLawerUsername(phonenumber);
		lawer.setLawerPhone(phonenumber);
		System.out.println("phonenumber");
		lawer.setLawerPassword(password);
		lawer.setLawerName(lawyername);
		System.out.println("lawyername");
		lawer.setLawerPinyinName(PinyinUtils.getPinYin(lawyername));
		lawer.setLawerEmail(emial);
		lawer.setLawerIdnumber(idnumber);
		lawer.setLawerIdcardnumber(license);
		lawer.setLawerFirm(lawfirm);
		lawer.setLawerProvince(province);
		lawer.setLawerCity(city);
		lawer.setLawerCreateTime(nousedate);
		lawer.setLawerLastLoginTime(nousedate);
		System.out.println("nousedate");
		
		ilLawerDAO.save(lawer);
		System.out.println("save");
		jsonObj.put("code", "true");
	}
	json = jsonObj.toString();
	return SUCCESS;
}
	public String  lawyervalidation() throws Exception {                    //本所律师验证登录
		
//		我没记错的话 get方式提交的参数编码，只支持iso8859-1编码。
//		如果非要用这种方式的话建议：
		String vertify = (String)session.get("ver");                       //从session中得到验证码
		jsonObj.put("code", "false");		                          
		json = jsonObj.toString(); 											//默认验证码验证错误
		if(vertify!=null&&vertify.equals(vertification))			//验证 验证码是否正确
		{	
			jsonObj.put("code", "true");							//验证码验证正确

		}
		else return SUCCESS;; 											//验证码错误
		IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ac); 	//获得DAO	
		ArrayList list=(ArrayList) ilLawerDAO.findByLawerUsername(phonenumber); //本所律师验证时 检查 手机号是否已经使用过，毕竟用手机号作为用户名
		if(list!=null&&!list.isEmpty()){										//手机号已经使用过，即用户名已经使用过			
			jsonObj.put("code", "exist");
			json = jsonObj.toString();
			return SUCCESS;
		}
		list=(ArrayList) ilLawerDAO.findByLawerIdcardnumber(license);      //检查律师证号是否存在
		if(list==null||list.isEmpty()){
			jsonObj.put("code", "noexist");									//律师证号不存在
		}
		else {				
			if(lawyername!=null&&!lawyername.equals(((IlLawer)(list.get(0))).getLawerName())){		//检查姓名是否匹配
				jsonObj.put("code", "noright");
			}
		}
		/*else if(name.equals(((IlLawer) list.get(0)).getLawerName())){   //此处应该添加姓名匹配，因为DAO中没有name，所以以后再来修改
				System.out.println("本所律师验证成功");
				jsonObj.put("code", "true");
			}*/
		json = jsonObj.toString();
		return SUCCESS;
}
	public String  vertifyfindpasswordback() throws Exception {				//密码找回页面的验证码输入action对应的函数
	
	String vertify = (String)session.get("ver");
	jsonObj.put("code", "false");
	json = jsonObj.toString();
	if(vertify!=null&&vertify.equals(vertification))
	{	
		jsonObj.put("code", "true");
	}
	else return SUCCESS; //验证码错误code为false
	IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ac);
	ArrayList list=(ArrayList) ilLawerDAO.findByLawerUsername(phonenumber); //找不到该用户名则报错，
	if(list==null||list.isEmpty()){
		jsonObj.put("code", "noexist");
	}
	json = jsonObj.toString();
	return SUCCESS;
}
	
	public String  passwordfindback() throws Exception {				//密码重新输入函数
	IlLawerDAO ilLawerDAO=IlLawerDAO.getFromApplicationContext(ac);
	ArrayList list=(ArrayList) ilLawerDAO.findByLawerUsername(phonenumber); 
	if(list!=null&&!list.isEmpty()){
		//ilLawerDAO.delete((IlLawer) list.get(0));    //删除原纪录
		((IlLawer) list.get(0)).setLawerPassword(password);
		//((IlLawer) list.get(0)).setLawerId(lawer_id);
		ilLawerDAO.attachDirty((IlLawer) list.get(0));    //存入修改过密码的记录
		jsonObj.put("code", "true");			//存入true
		json = jsonObj.toString();
	}
	else{
		jsonObj.put("code", "false");			//存入false
		json = jsonObj.toString();
	}
	return SUCCESS;
}
	public String  jump() throws Exception {  	//用于action页面跳转返回SUCCESS
		return SUCCESS;
	}
	public String getVertification() {
		return vertification;
	}

	public void setVertification(String vertification) {
		this.vertification = vertification;
	}
	public String getPhonenumber() {
		return phonenumber;
	}

	public void setPhonenumber(String phonenumber) {
		this.phonenumber = phonenumber;
	}
	public JSONObject getJsonObj() {    //没什么用处
		return jsonObj;
	}
	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public String getPassword() {
		return phonenumber;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmial() {
		return emial;
	}

	public void setEmial(String emial) {
		this.emial = emial;
	}
	public String getLawyername() {
		return lawyername;
	}

	public void setLawyername(String lawyername) {
		this.lawyername = lawyername;
	}
	public String getLawfirm() {
		return lawfirm;
	}

	public void setLawfirm(String lawfirm) {
		this.lawfirm = lawfirm;
	}
	public String getIdnumber() {
		return idnumber;
	}

	public void setIdnumber(String idnumber) {
		this.idnumber = idnumber;
	}
	public String getLicense() {
		return license;
	}

	public void setLicense(String license) {
		this.license = license;
	}
	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

}